/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.shrikanthravi.customnavigationdrawer.slice;

import com.shrikanthravi.customnavigationdrawer.ResourceTable;
import com.shrikanthravi.customnavigationdrawer.fraction.NewsFraction;
import com.shrikanthravi.customnavigationdrawer2.data.MenuItem;
import com.shrikanthravi.customnavigationdrawer2.utils.FractionChangeManager;
import com.shrikanthravi.customnavigationdrawer2.widget.SNavigationDrawer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 *
 * @author wangyin
 * @since 2021/06/23
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * SNavigationDrawer
     */
    private SNavigationDrawer mSNavigationDrawer;

    /**
     * mFragments
     */
    private final ArrayList<Fraction> mFragments = new ArrayList<>();

    /**
     * FractionChangeManager
     */
    private FractionChangeManager manager;

    /**
     * contents
     */
    private final String[] content = {"News Fragment", "Feed Fragment", "Messages Fragment", "Music Fragment"};

    /**
     * colors
     */
    private final int[] colors = {ResourceTable.Color_red,
            ResourceTable.Color_orange,
            ResourceTable.Color_green,
            ResourceTable.Color_colorBlue};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Component component = findComponentById(ResourceTable.Id_navigationDrawer);
        if (component instanceof SNavigationDrawer) {
            mSNavigationDrawer = (SNavigationDrawer) component;
        }
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(new MenuItem("News", ResourceTable.Media_news_bg));
        menuItems.add(new MenuItem("Feed", ResourceTable.Media_feed_bg));
        menuItems.add(new MenuItem("Messages", ResourceTable.Media_message_bg));
        menuItems.add(new MenuItem("Music", ResourceTable.Media_music_bg));
        mSNavigationDrawer.setMenuItemList(menuItems);
        initFragment(menuItems);
        if (getAbility() instanceof FractionAbility) {
            FractionAbility ability = (FractionAbility) getAbility();
            manager = new FractionChangeManager(ability.getFractionManager(), ResourceTable.Id_frameLayout, mFragments);
        }
        mSNavigationDrawer.setDrawerListener(new SNavigationDrawer.DrawerListener() {
            @Override
            public void onDrawerOpening() {
            }

            @Override
            public void onDrawerClosing() {
            }

            @Override
            public void onDrawerOpened() {
            }

            @Override
            public void onDrawerClosed() {
            }

            @Override
            public void onDrawerStateChanged(SNavigationDrawer.State newState) {
            }
        });
        mSNavigationDrawer.setOnMenuItemClickListener(position -> manager.setFragments(position));
    }

    private void initFragment(List<MenuItem> menuItems) {
        for (int i = 0; i < menuItems.size(); i++) {
            mFragments.add(NewsFraction.getInstance(content[i], colors[i]));
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
